//
//  HLHomeCell.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/20.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit
import SnapKit

class HLHomeCell: UITableViewCell {
    
    var BottomViewTopConstraints: Constraint?
    
    //接收状态数据的属性
    var homeStatus: HLHomeStatuses?{
        didSet {
            //给子控件设置数据
            homeCellTopView.homeStatus = homeStatus
            homeCellRetweetView.retweetStatus = homeStatus?.retweeted_status
            //卸载约束
            BottomViewTopConstraints?.uninstall()
            //根据有没有转发微博来设置约束
            if let _ = homeStatus?.retweeted_status {
                //更新约束
                //有转发微博
                homeCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    BottomViewTopConstraints = make.top.equalTo(homeCellRetweetView.snp_bottom).constraint
                    homeCellRetweetView.hidden = false
                })
            }else {
                homeCellBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    BottomViewTopConstraints = make.top.equalTo(homeCellTopView.snp_bottom).constraint
                    //影藏
                    homeCellRetweetView.hidden = true
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置界面
    private func setupUI() {
        //设置选中状态下样式
        selectionStyle = .None
        //添加分隔视图
        contentView.addSubview(homeCellSeparateView)
        //添加子控件
        contentView.addSubview(homeCellTopView)
        contentView.addSubview(homeCellBottomView)
        contentView.addSubview(homeCellRetweetView)
        
        //设置约束
        //分隔视图
        homeCellSeparateView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self.contentView)
            make.height.equalTo(10)
        }
        //头部视图
        homeCellTopView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeCellSeparateView.snp_bottom)
            make.left.right.equalTo(self.contentView)
            
        }
        //转发视图
        homeCellRetweetView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(self.contentView)
            make.top.equalTo(homeCellTopView.snp_bottom)
        }
        //底部视图
        homeCellBottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(self.contentView)
            BottomViewTopConstraints = make.top.equalTo(homeCellRetweetView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        //设置contentView的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(homeCellBottomView.snp_bottom)
        }
    }
    
    // MARK: 懒加载子控件
    //头部视图
    private lazy var homeCellTopView: HLHomeCellTopView = HLHomeCellTopView()
    //底部视图
    private lazy var homeCellBottomView: HLHomeCellBottomView = HLHomeCellBottomView()
    //转发视图
    private lazy var homeCellRetweetView: HLHomeCellRetweetView = HLHomeCellRetweetView()
    //分隔视图
    private lazy var homeCellSeparateView: UIView = {
       let sView = UIView()
        sView.backgroundColor = UIColor(white: 0.8, alpha: 1)
        return sView
    }()
    
}
